package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.StartAndEnd;
import com.itheima.service.StatisticsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Reference
    private StatisticsService statisticsService;

    @RequestMapping("/dailyAppointment")
    public Result dailyAppointment(@RequestBody StartAndEnd startAndEnd) {
        try {
//        获取传入的开始时间和结束时间
            System.out.println(startAndEnd.getStart());
            System.out.println(startAndEnd.getEnd());
            long start = startAndEnd.getStart().getTime();
            long end = startAndEnd.getEnd().getTime();
//        把开始时间和结束时间相减获得毫秒值，再除即可得到天数
            Long monthCount = (end - start) / (3600 * 1000 * 24);
            //        通过时间获取预约和到诊情况
//            把传来的日期转换成字符串在转换成日期格式yyyy-MM-dd
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(start));
            Map<String, Object> countByTime = statisticsService.findAppointmentCountByTime(parse, monthCount);
            return new Result(true, "数据统计查询成功", countByTime);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "数据统计查询失败");
        }
    }

    @RequestMapping("/packageIncome")
    public Result packageIncome(@RequestBody StartAndEnd startAndEnd) {
        //根据json返回数据格式可知，data需要用map集合
        Map<String, Object> data = new HashMap<>();
        try {
//        获取一段时间内套餐的收入
            List<Map<String, Object>> list = statisticsService.packageIncome(startAndEnd);
//            遍历套餐收入数据
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map : list) {
//                为侧边框赋值
                setmealNames.add((String) map.get("name"));

            }
            data.put("setmealNames", setmealNames);
            data.put("setmealIncome", list);
            return new Result(true, "数据统计查询成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "数据统计查询失败");
        }
    }

    @RequestMapping("/ageDistribution")
    public Result ageDistribution() {
        try {
            Map<String, Object> map = statisticsService.ageDistribution();
            return new Result(true, "", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "");
        }
    }

    @RequestMapping("/personnelDistribution")
    public Result personnelDistribution(){
        try {
            List list = statisticsService.personnelDistribution();
            return new Result(true, "查询人员占比成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询人员占比失败");
        }
    }
}
